package cn.changeforyou.analyzer.javaparse.model;

import cn.changeforyou.analyzer.CombineElement;
import cn.changeforyou.analyzer.combine.orm.compare.ComparedStruct;
import cn.changeforyou.analyzer.naming.NamingStrategy;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * @author zhyu
 * @date 2021/2/9 13:30
 */
@Data
@ToString
public class EntityModel implements ComparedStruct {

    private String tableName;

    private String tableComment;

    private List<FieldInfo> fieldInfos;
    
    private ClassOrInterfaceDeclaration classOrInterfaceDeclaration;
    
    private File file;
    
    @Override
    public List<String> getComparedStruct() {
        List<String> names = fieldInfos.stream().map(x -> x.getName(getNamingStrategy())).collect(Collectors.toList());
        Collections.sort(names);
        return names;
    }
    
    @Override
    public NamingStrategy getNamingStrategy() {
        return NamingStrategy.camelCase;
    }
    
    @Override
    public String getStructName() {
        return tableName;
    }
    
    @Getter@Setter
    public static class FieldInfo implements CombineElement {

        private boolean isPrimaryKey;
        
        private String javaType;

        private String name;

        private String comment;
        
        private String column;
        
        private EntityModel entityModel;
    
        public FieldInfo(EntityModel entityModel) {
            this.entityModel = entityModel;
        }
    
        @Override
        public ComparedStruct getParentStruct() {
            return entityModel;
        }
    
        @Override
        public String toString() {
            return "FieldInfo{" +
                       "javaType='" + javaType + '\'' +
                       ", name='" + name + '\'' +
                       ", comment='" + comment + '\'' +
                       ", column='" + column + '\'' +
                       ", mybatisPlusEntityModel=@" + entityModel.hashCode() +
                       '}';
        }
    }
}
